package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.*
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivityWdgpBinding
import client.comm.baoding.dialog.GuaMaiDialog
import client.comm.baoding.dialog.ZhaiMaiDialog
import client.comm.baoding.ui.vm.WdgpViewModel
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.dialog.ValidatePwdDialog
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.commlib.widget.EmptyLayout
import kotlinx.coroutines.flow.*

class WdgpActivity : BaseMiddleActivity<WdgpViewModel, ActivityWdgpBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_wdgp
    }

    val adapter: MyGuaDAndSgAdapter by lazy {
        MyGuaDAndSgAdapter(mImgLoader).apply {

            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA
                } else {
                    binding.emptyLayout.visibility = View.GONE
                }
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                toast((it).error.message)
            }
            guaMaiCli = { o, t ->

                if (t == 1) {

                    ZhaiMaiDialog(this@WdgpActivity).apply {
                        showNum.set(true)
                        okPay = {
                            if (pwd.get().isNullOrEmpty()) {
                                toast("请输入支付密码")
                            } else if (num.get().isNullOrEmpty()) {
                                toast("请输入份额")
                            } else
                                viewModel.zmaiClick(pwd.get()!!, o.id, num.get()!!, "${it}")

                        }
                    }.show()
                } else
                    GuaMaiDialog(this@WdgpActivity).apply {
                        okPay = {
                            if (pwd.get().isNullOrEmpty()) {
                                toast("请输入支付密码")
                            } else if (num.get().isNullOrEmpty()) {
                                toast("请输入份额")
                            } else
                                viewModel.zmaiClick(pwd.get()!!, o.id, num.get()!!, "1")

                        }
                    }.show()

//                ValidatePwdDialog(this@WdgpActivity).apply {
//                    title = "需要验证支付密码"
//                    ok = {
//                        viewModel.zmaiClick(it, o.id)
//                    }
//                }.show()
            }
        }
    }

    override fun initView() {
        binding?.apply {
            event = this@WdgpActivity
            vm = viewModel
        }
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.refresh()
        }
        binding.recyclerView.adapter = adapter.withLoadStateFooter(
            footer = PagerFooterAdapter()
        )
//
    }


    override fun onResume() {
        super.onResume()
        lifecycleScope.launchWhenResumed {
            viewModel.pagingDataAuto {
                if (it == 1) {
                    val ret = viewModel.server.trade_hall(it)
                    ret.body()?.let {
                        binding.curPrice = it.data?.stock_price
                        binding.myFe = it.data?.income_r
                        binding.curVal = it.data?.total_stock

                    }
                    PagingJsonResult(
                        list = ret.body()?.data?.order_list,
                        code = ret.body()?.code,
                        message = ret.body()?.message,
                        errorStr = ret.errorBody()?.string()
                    )
                } else
                    PagingJsonResult(
                        list = arrayListOf(),
                        code = 200,
                        message = ""
                    )
            }.collect {
                adapter.submitData(it)
            }
        }
    }

    override fun observerUI() {
        viewModel.gmMyRet.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    toast("操作成功")
                    viewModel.guadanJe.value = ""
                    adapter.refresh()
                } else {
                    toast(it.message)
                }
                viewModel.gmMyRet.value = null
            }
        })
        viewModel.clickGmRet.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    toast("操作成功")
                    startActivity(Intent(this, GpjlActivity::class.java))
                } else {
                    toast(it.message)
                }
                viewModel.gmMyRet.value = null
            }
        })
        viewModel.hzGmRet.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    toast("操作成功")
                    startActivity(Intent(this, GpjlActivity::class.java))
                } else {
                    toast(it.message)
                }
                viewModel.gmMyRet.value = null

            }
        })
    }

    override fun initViewModel(): WdgpViewModel = createViewModel()

    fun jl() {
        startActivity(Intent(this, GpjlActivity::class.java))
    }

    fun wdgd() {
        startActivity(Intent(this, MyGuaDan2Activity::class.java))
    }

    fun gmClick() {
        if (viewModel.guadanJe.value.isNullOrEmpty()) {
            toast("请输入挂买份额数量")
            return
        }
        ZhaiMaiDialog(this).apply {
            showNum.set(false)
            title = "挂买"
            okPay = {
                if (pwd.get().isNullOrEmpty()) {
                    toast("请输入支付密码")
                }  else
                    viewModel.gmClick(pwd.get()!!, "${it}")
            }
        }.show()
//        ValidatePwdDialog(this).apply {
//            title = "需要验证支付密码"
//            ok = {
//                viewModel.gmClick(it)
//            }
//        }.show()
    }

    fun gmaiClick() {
        if (viewModel.guadanJe.value.isNullOrEmpty()) {
            toast("请输入挂卖份额数量")
            return
        }
        ValidatePwdDialog(this).apply {
            title = "需要验证支付密码"
            ok = {
                viewModel.gmaiClick(it)
            }
        }.show()
    }

    fun hzClick() {
        if (viewModel.huzhuanJe.value.isNullOrEmpty()) {
            toast("请输入互转份额数量")
            return
        }
        if (viewModel.duifangSjh.value.isNullOrEmpty()) {
            toast("请输入对方手机号")
            return
        }
        ValidatePwdDialog(this).apply {
            title = "需要验证支付密码"
            ok = {
                viewModel.hz(it)
            }
        }.show()
    }
}